#!/usr/bin/env python
# -*- coding: utf-8 -*-


class ProjectMgr:
    def __init__(self, app, session):
        self.app = app
        self.session = session
        self.projects = [
            {
                "id": 111111, 
                "name": "Mock项目", 
                "owner": "admin", 
                "type": "uitest", 
                "platform": "ohos",
                "version": "5.0",
                "create": "2023-10-01", 
                "edit": "2024-09-28", 
                "publish": "2025-09-28",
                "desc": "Mock项目描述",
            },
            {
                "id": 222222, 
                "name": "Mock项目", 
                "owner": "admin",
                "type": "uitest",
                "platform": "hos",
                "version": "5.1",
                "create": "2023-10-05",
                "edit": "2024-10-03",
                "publish": "2025-10-03",
                "desc": "Mock项目描述",
            },
            {
                "id": 333333,
                "name": "Mock项目",
                "owner": "admin",
                "type": "uitest",
                "platform": "khos",
                "version": "4.1",
                "create": "2023-09-20",
                "edit": "2024-09-15",
                "publish": "2025-09-15",
                "desc": "Mock项目描述",
            },
        ]


    def destroy(self):
        self.session = None
        self.app = None

    
    def __del__(self):
        self.destroy()


    def get_projects(
        self,
        search = None,
        owner = None,
        type = None,
        platform = None,
        version = None,
        start_date = None,
        end_date = None):
        if owner == "All":
            owner = None
        if type == "All":
            type = None
        if platform == "All":
            platform = None
        if search or owner or type or platform or version or start_date or end_date:
            return { "code": 200, "content": self.projects[0:1] }
        return { "code": 200, "content": self.projects }
    
    def get_project_detail(self, id: str):
        for project in self.projects:
            if str(project["id"]) == id:
                return { "code": 200, "content": project }
        return { "code": 404, "content": {"message": "项目不存在"} }

    
    def open_project(self, id: int):
        for project in self.projects:
            if project["id"] == id:
                return { "code": 200, "content": project }
        return { "code": 404, "content": {"message": "项目不存在"} }

    
    def rename_project(self, id: str, name: str):
        for project in self.projects:
            if str(project["id"]) == id:
                project["name"] = name
                return { "code": 200, "content": project }
        return { "code": 404, "content": {"message": "项目不存在"} }

    def update_project(self, id, name, type, platform, version, desc):
        for project in self.projects:
            if str(project["id"]) == id:
                project["name"] = name
                project["type"] = type
                project["platform"] = platform
                project["version"] = version
                project["desc"] = desc
                return { "code": 200, "content": project }
        return { "code": 404, "content": {"message": "项目不存在"} }


    def run_project(self, id):
        return { "code": 200, "content": {} }


    def new_project(self, name, type, platform, version, desc):
        return {
            "code": 200,
            "content": {
                "id": 123456,
                "name": name,
                "owner": "admin",
                "type": type,
                "platform": platform,
                "version": version,
                "create": "2023-10-01",
                "edit": "2024-09-28",
                "publish": "2025-09-28",
                "desc": desc,
            }
        }
        

    def delete_project(self, id):
        return { "code": 200, "content": {} }


    def import_projects(self, projects):
        return { "code": 200, "content": {} }


    def export_projects(self, projects):
        return { "code": 200, "content": {} }


    def get_test_cases(self, id: str):
        return { 
            "code": 200, 
            "content": [
                {
                    "id": 111111,
                    "name": "Mock测试用例",
                },
                {
                    "id": 222222,
                    "name": "Mock测试用例",
                },
            ]
        }

    
    def get_test_tasks(self, id: str):
        return {
            "code": 200,
            "content": [
                {
                    "id": 111111,
                    "name": "Mock测试任务",
                },
                {
                    "id": 222222,
                    "name": "Mock测试任务",
                },
            ]
        }